package com.liangdemo.web.aop;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 定义一个切面类
 *
 * @author Mr.Liang
 * @date 2022/5/26 10:40
 */
@Aspect
@Component
public class myAdvie {
    private  String name = "xxxxxxx很爱很爱你";
    private Logger logger = LoggerFactory.getLogger(myAdvie.class);

    /**
     * 定义切面
     */
    @Pointcut("execution(public  * com.demo.liangdemo.web.aop.*.*(..))")
    public void myPointcut() {

    }

    /**
     * 定义通知，环绕通知
     */
    @Around("myPointcut()")
    public Object myLogger(ProceedingJoinPoint pjp) throws Throwable {
        //获取类名
        String className = pjp.getTarget().getClass().toString();
        //获取方法名
        String methodName = pjp.getSignature().getName();
        //获取参数
        Object[] array = pjp.getArgs();

        ObjectMapper mapper = new ObjectMapper();

        logger.info("调用前：" + className + ":" + "传递的参数" + ":" + mapper.writeValueAsString(array));
        Object obj = pjp.proceed();
        logger.info("调用后：" + className + ":" + methodName + "返回值：" + mapper.writeValueAsString(obj));

        return obj;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
